package org.example.flinktest.apidemo.sinktest;

import java.util.Properties;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer010;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer010;
import org.example.flinktest.bean.SensorReading;

/**
 * @author shihb
 * @date 2019/12/6 18:45
 */
public class KafkaSinkDemo {


  public static void main(String[] args) throws Exception {

    //环境
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

    //source
    Properties properties = new Properties();
    properties.setProperty("bootstrap.servers",
        "192.168.7.201:9092,192.168.7.202:9092,192.168.7.203:9092");
    properties.setProperty("group.id", "consumer-group");
    properties.setProperty("key.deserializer",
        "org.apache.kafka.common.serialization.StringDeserializer");
    properties.setProperty("value.deserializer",
        "org.apache.kafka.common.serialization.StringDeserializer");
    properties.setProperty("auto.offset.reset", "latest");
    DataStream<String> input = env
        .addSource(new FlinkKafkaConsumer010<String>("test", new SimpleStringSchema(), properties));

    //transform操作
    DataStream<String> dataStream = input.map(new MapFunction<String, String>() {
      @Override
      public String map(String s) {
        String[] arr = s.split(",");
        SensorReading sensorReading = new SensorReading(arr[0], Long.parseLong(arr[1]),
            Double.parseDouble(arr[2]));
        return sensorReading.toString();
      }
    });

    //sink
    dataStream.addSink(new FlinkKafkaProducer010<>("broklist",
        "topic",
        new SimpleStringSchema()));

    env.execute("kafka sink test");


  }

}
